package system

import (
	"github.com/gin-gonic/gin"
	"vben-api/api/account"
	"vben-api/pkg/middleware/auth"
	"vben-api/pkg/svc"
	"vben-api/router"
)

type RouteHandler struct {
}

func init() {
	router.Register(NewRouteMenu())
}

func NewRouteMenu() *RouteHandler {
	return &RouteHandler{}
}

func (ru *RouteHandler) Route(r *gin.Engine, svcCtx *svc.ServiceContext) {
	ru.menuRoute(r, svcCtx).departmentRoute(r, svcCtx).accountRoute(r, svcCtx).
		roleRoute(r, svcCtx).
		userRoute(r, svcCtx).
		interfaceRoute(r, svcCtx).
		buttonRoute(r, svcCtx).captchaRoute(r, svcCtx)
}

func (ru *RouteHandler) captchaRoute(r *gin.Engine, svcCtx *svc.ServiceContext) *RouteHandler {
	capt := account.NewCaptchaApi(svcCtx)
	r.GET("/captcha", capt.GenerateCaptchaHandle)
	r.POST("/captcha/verify", capt.CaptchaVerifyHandle)
	r.GET("/uuid", capt.GetUuid)
	return ru
}

func (ru *RouteHandler) accountRoute(r *gin.Engine, svcCtx *svc.ServiceContext) *RouteHandler {
	acc := account.NewAccount(svcCtx)
	accGroup := r.Group("account")
	{
		accGroup.POST("/login", acc.Login)
		accGroup.GET("/logout", acc.Logout)
		accGroup.POST("/refresh", acc.RefreshToken)
		accGroup.Use(auth.JWTAuthMiddleware(svcCtx))
		{
			accGroup.GET("/profile", acc.Profile)
		}
	}
	return ru
}

func (ru *RouteHandler) departmentRoute(r *gin.Engine, svcCtx *svc.ServiceContext) *RouteHandler {
	department := NewDepartment(svcCtx)
	deptGroup := r.Group("dept").Use(auth.JWTAuthMiddleware(svcCtx))
	{
		deptGroup.GET("/info", department.GetDeptInfo)
		deptGroup.GET("/list", department.GetDeptList)
		deptGroup.POST("/create", department.CreateDept)
		deptGroup.POST("/del", department.DeleteDept)
		deptGroup.POST("/update", department.UpdateDept)
	}
	return ru
}

func (ru *RouteHandler) menuRoute(r *gin.Engine, svcCtx *svc.ServiceContext) *RouteHandler {
	menu := NewMenu(svcCtx)
	menuGroup := r.Group("menu")
	{
		menuGroup.Use(auth.JWTAuthMiddleware(svcCtx))
		{
			menuGroup.GET("/info", menu.GetMenuInfo)
			menuGroup.GET("/list", menu.GetMenuList)
			menuGroup.POST("/save", menu.SaveMenu)
			menuGroup.POST("/update", menu.UpdateMenu)
			menuGroup.DELETE("/delete", menu.DeleteMenu)
			menuGroup.GET("/select/tree", menu.SelectMenuTree)
			menuGroup.GET("/nav/list", menu.GetNavList)
		}
	}
	return ru
}

// roleRoute
// @Description: 角色路由
// @receiver ru
// @param r
func (ru *RouteHandler) roleRoute(r *gin.Engine, svcCtx *svc.ServiceContext) *RouteHandler {
	role := NewRole(svcCtx)
	roleGroup := r.Group("role")
	{
		roleGroup.Use(auth.JWTAuthMiddleware(svcCtx))
		{
			roleGroup.GET("info", role.GetRoleInfo)
			roleGroup.GET("list", role.GetRoleList)
			roleGroup.POST("save", role.SaveRole)
			roleGroup.POST("update", role.UpdateRole)
			roleGroup.POST("set/status/:id", role.SetRoleStatus)
			roleGroup.DELETE("delete", role.DeleteRole)
			roleGroup.POST("binding/interface", role.CreateRoleInterface)
			roleGroup.GET("/interface/list/:role_id", role.GetRoleInterfaceList)
			roleGroup.POST("binding/button", role.CreateRoleButton)
			roleGroup.GET("/button/list/:role_id", role.GetRoleButtonList)
			roleGroup.GET("/getPermCode", role.GetPermCode)
		}
	}
	return ru
}

// userRoute
// @Description: 用户相关路由
// @receiver ru
// @param r
// @return *RouteHandler
func (ru *RouteHandler) userRoute(r *gin.Engine, svcCtx *svc.ServiceContext) *RouteHandler {
	user := NewUser(svcCtx)
	userGroup := r.Group("user")
	{
		userGroup.Use(auth.JWTAuthMiddleware(svcCtx))
		{
			userGroup.GET("info/:id", user.GetUserInfo)
			userGroup.GET("list", user.GetUserList)
			userGroup.POST("save", user.SaveUser)
			userGroup.POST("/update/:id", user.UpdateUser)
			userGroup.DELETE("delete", user.DeleteUser)
			userGroup.POST("/change/pass", user.ChangePassword)
		}
	}
	return ru
}

// interfaceRoute
// @Description: 接口相关路由
// @receiver ru
// @param r
// @return *RouteHandler
func (ru *RouteHandler) interfaceRoute(r *gin.Engine, svcCtx *svc.ServiceContext) *RouteHandler {
	inter := NewInterfaces(svcCtx)
	interfaceGroup := r.Group("interface")
	{
		interfaceGroup.Use(auth.JWTAuthMiddleware(svcCtx))
		{
			interfaceGroup.GET("info/:id", inter.GetInterfaceInfo)
			interfaceGroup.GET("list", inter.GetInterfaceList)
			interfaceGroup.POST("save", inter.SaveInterface)
			interfaceGroup.POST("/update/:id", inter.UpdateInterface)
			interfaceGroup.DELETE("delete", inter.DeleteInterface)
			interfaceGroup.GET("all", inter.GetAll)
		}
	}
	return ru
}

func (ru *RouteHandler) buttonRoute(r *gin.Engine, svcCtx *svc.ServiceContext) *RouteHandler {
	btn := NewButton(svcCtx)
	btnGroup := r.Group("button")
	{
		btnGroup.Use(auth.JWTAuthMiddleware(svcCtx))
		{
			btnGroup.GET("info/:id", btn.GetButtonInfo)
			btnGroup.GET("list", btn.GetButtonList)
			btnGroup.POST("save", btn.SaveButton)
			btnGroup.POST("/update/:id", btn.UpdateButton)
			btnGroup.DELETE("delete", btn.DeleteButton)
			btnGroup.GET("all", btn.GetAll)
		}
	}
	return ru
}
